//
//  LDCheckPublicInfo.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/4/26.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

class LDCheckPublicInfo: NSObject {
    
    var manger = LDUserInfo.sharedInstance
    //入口
    class var shareInstance:LDCheckPublicInfo {
        struct Staics {
            static let instance = LDCheckPublicInfo()
        }
        return Staics.instance
    }
    
    //查询 app 系统设置
    func queryAppSystemConfigure(succ:((value:[AnyObject])->()),fail:((data:AnyObject)->Void)?) {
        
        
        let api = LDCommentAPI.appConfigure
        
        LDNetWorkManager.sharedInstance.GET(api, header: nil, success: { (value) in
            
            let arrays = value as? [AnyObject] ?? [AnyObject]()
            
            LDCacheManager.shareInstance.saveToCache("appconfigureinfo", value: arrays as [AnyObject], success: nil)
            succ(value: arrays)
            
            
        }) { (data, error) in
            
            LDCacheManager.shareInstance.readFromCache("appconfigureinfo", colsure: { (key, value) in
                
                if value != nil {
                    
                    succ(value: value as? [AnyObject] ?? [AnyObject]())
                } else {
                    
                    if fail != nil {
                        fail!(data: data)
                    }
                }
                
            })
        }
    }
    
    //configure
    func queryAppXMLConfigure(closure:(data:NSData)->(),failure:(()->())?) {
        
//        let key = "queryAppConfigureXMLInfo"
        
        let api = LDCommentAPI.appConfigureXML
//        let api = "https://127.0.0.1/app/LDAPPConfig.xml"
//        if manger.userCacheManger?.containsObjectForKey(key) == true {
//            
//            LDUserInfo.sharedInstance.readFromCache(key, success: { (key, value) in
//                
//            
//            }, failure: nil)
//            
//        } else {
        
            LDNetWorkManager.sharedInstance.GETXML(api, success: { (value) in
                
                closure(data: value)
                LDCacheManager.shareInstance.saveToCache(LDAppXMLCacheKey, value: value, success: nil)
            }, failure: { (data, error) in
                if failure != nil { failure!() }
            })
//        }
    }
    
    //查询所有dd+的信息
    func queryWorthContent(isCache:Bool,succ:((value:[AnyObject])->()),fail:((data:AnyObject)->Void)?) {
        
        let api = LDCommentAPI.queryDDPlus
        let key = "ddplusinfocache"
        
        if isCache == false {
            
            
            LDNetWorkManager.sharedInstance.GET(api, header: nil, success: { (value) in
                
                succ(value: value as? [AnyObject] ?? [AnyObject]())
                LDCacheManager.shareInstance.saveToCache(key, value: value as! [AnyObject], success: nil)
                
            }, failure: { (data, error) in
                
                if fail != nil {
                    fail!(data: data)
                }
            })
            
            return
        }
        
        //暂时兼容性写法
        let value = LDCacheManager.shareInstance.userCacheManger?.objectForKey(key)
        if value != nil {
            
            succ(value: value as? [AnyObject] ?? [AnyObject]())
            return
        }
    
        LDNetWorkManager.sharedInstance.GET(api, header: nil, success: { (value) in
            
            let arrays = value as? [AnyObject] ?? [AnyObject]()
            
            LDCacheManager.shareInstance.saveToCache(key, value: arrays as [AnyObject], success: nil)
            succ(value: arrays)
            
            
        }) { (data, error) in
            
            LDCacheManager.shareInstance.readFromCache(key, colsure: { (key, value) in
                
                if value != nil {
                    
                    succ(value: value as? [AnyObject] ?? [AnyObject]())
                } else {
                    
                    if fail != nil {
                        fail!(data: data)
                    }
                }
                
            })
        }
    }
    
    //客服端上传个推 clientid
    internal func uploadGeTuClientID(params:NSDictionary,succ:(()->Void)?,fail:((data:AnyObject)->Void)?) {
        
        LDNetWorkManager.sharedInstance.LoginPOST(LDJobfinderAPI.set.upLoadGeTuClient, parameters: params as? Dictionary<String, AnyObject>, success: { (value) in
            
            if succ != nil {
                succ!()
            }
        }) { (data, error) in
            if fail != nil {
                fail!(data: data)
            }
        }
        
    }
    
    
    //重置密码
    func resetPassword(params:[String:String],succ:(()->Void)?,fail:((data:AnyObject)->Void)?) {
        
        LDNetWorkManager.sharedInstance.POST(LDCommentAPI.resetPassword, parameters: params, success: { (value) in
            
            if succ != nil {
                succ!()
            }
            
//            print(value)
            
        }) { (data, error) in
            
            if fail != nil {
                fail!(data: data)
            }
            
//            debugPrint(data)
        }
        
    }
    //登录注册
    func registerUserInfo(params:[String:String],succ:((value:AnyObject)->Void),fail:((data:AnyObject)->Void)) {
        
        let api = LDCommentAPI.registerUserInfo + "?" + "mobile=" + params["mobile"]! + "&password=" + params["password"]! + "&code=" + params["code"]!
        
        LDNetWorkManager.sharedInstance.GET(api, header: nil, success: { (value) in
            
            succ(value: value)
            
        }) { (data, error) in
            
            fail(data: data)
        }
    }
    
    //验证码获取
    func getSecurityCode(phone:String,succ:(()->Void)?,fail:((data:AnyObject)->Void)?) {
        
        let api = LDCommentAPI.getSecurityCode + phone
        LDNetWorkManager.sharedInstance.GET(api, header: nil, success: { (value) in
            
            if succ != nil {
                succ!()
            }
            
        }) { (data, error) in
            
            if fail != nil {
                fail!(data: data)
            }
        }
    }
    
    //快捷登录
    func expressLogin(params:NSDictionary,succ:((value:NSDictionary)->Void),fail:((data:AnyObject)->Void)?) {
        
        let api = LDCommentAPI.expressLogin
        
        LDNetWorkManager.sharedInstance.POST(api, parameters: params as? Dictionary<String, AnyObject>, success: { (value) in
            
            succ(value: value as! NSDictionary)
            
        }) { (data, error) in
            if fail != nil {
                fail!(data: data)
            }
            
        }
    }
    
    
    /**查询所有省份信息*/
    func checkoutAllProvienceInfo(closure:(data:Array<Dictionary<String,String>>)->Void) {
        
        if manger.userCacheManger?.containsObjectForKey("ProvienceInfo") == true {
            
            LDUserInfo.sharedInstance.readFromCache("ProvienceInfo", success: { (key, value) in
                closure(data: value as! Array<Dictionary<String, String>>)
                }, failure: nil)
            
        } else {
            
            LDNetWorkManager.sharedInstance.GET(LDCommentAPI.provinces, header: nil,
                                                success: { [weak self] (value) in
                                                    let data = value as! Array<Dictionary<String, String>>
                                                    closure(data: data)
                                                    self?.manger.saveToCache("ProvienceInfo",value: data, success: {
                                                    })
            }) { (data, error) in
//                print(data)
            }
        }
    }
    /**查询所有城市信息*/
    func checkoutAllCityInfo(closure:(data:Array<Dictionary<String,AnyObject>>)->Void) {
        
        
        if manger.userCacheManger?.containsObjectForKey("CityInfo") == true {
            
            LDUserInfo.sharedInstance.readFromCache("CityInfo", success: { (key, value) in
                closure(data: value as! Array<Dictionary<String, AnyObject>>)
                }, failure: nil)
            
        } else {
            
            LDNetWorkManager.sharedInstance.GET(LDCommentAPI.city, header: nil,
                                                success: { (value) in
                                                    let data = value as! Array<Dictionary<String, AnyObject>>
                                                    closure(data: data)
                                                    LDUserInfo.sharedInstance.saveToCache("CityInfo", value: data, success: {
                                                    })
                                                    
            }) { (data, error) in
//                print(data)
            }
        }
        
    }
    
    /**查询所有行业信息*/
    
    func checkoutAllJobIndustry(closure:(data:Array<Dictionary<String,String>>)->Void) {
        
        if manger.userCacheManger?.containsObjectForKey("JobFinderIndustryInfo") == true {
            
            LDUserInfo.sharedInstance.readFromCache("JobFinderIndustryInfo", success: { (key, value) in
                dispatch_async(dispatch_get_main_queue(), {
                    closure(data: value as! Array<Dictionary<String, String>>)
                })
                }, failure: nil)
            
            
        } else {
            
            LDNetWorkManager.sharedInstance.GET(LDCommentAPI.industries, header: nil,
                                                success: { (value) in
                                                    let data = value as! Array<Dictionary<String, String>>
                                                    
                                                    LDUserInfo.sharedInstance.saveToCache("JobFinderIndustryInfo", value: data, success: {
                                                    })
                                                    
                                                    dispatch_async(dispatch_get_main_queue(), {
                                                        closure(data: data)
                                                    })
                                                    
                                                    
                                                    
            }) { (data, error) in
                debugPrint(data)
            }
        }
    }
    /**查询所有职位信息*/
    func checkoutAllJobCategory(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.jobCategories, key: "JobCategoryInfo") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    /**查询所有薪资信息*/
    func checkoutAllSalary(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.salary, key: "AllSalaryInfo") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    func checkoutAllSalaryJobfinder(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.salaryJobfinder, key: "AllSalaryInfoJobfinder") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    
    /**查询所有学历信息*/
    func checkoutAllMainEduInfo(closure:(data:Array<NSDictionary>)->Void) {
        let api:String = LDCommentAPI.edu
        
        creatCache(api, key: "Queryedu") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    func checkoutAllMainEduInfoJobfinder(closure:(data:Array<NSDictionary>)->Void) {
        let api:String = LDCommentAPI.eduJobfinder
        
        creatCache(api, key: "QueryeduJobfinder") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    func checkoutAllMainEduInfoJobfinderEdit(closure:(data:Array<NSDictionary>)->Void) {
        let api:String = LDCommentAPI.eduJobfinderEdit
        
        creatCache(api, key: "QueryeduJobfinderEdit") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    /**查询所有公司规模信息*/
    
    /**查询工作重点*/
    
    func checkoutAllMainJobFocus(level2_id:String,closure:(data:Array<NSDictionary>)->Void) {
        let api:String = LDCommentAPI.level2jobcategory + level2_id
        //变动值
        creatCache(api, key: nil) { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    /**查询所有工作经验年限*/
    func checkoutAllExperience(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.experience, key: "JobExperience") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    //全部
    func checkoutAllExperienceJobfinder(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.jobExperience, key: "JobExperienceJobfinder") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    // 牛人 edit
    func checkoutAllExperienceJobfinderEdit(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.jobExperienceEdit, key: "JobExperienceJobfinderEdit") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    
    /**查询所有亮点标签 牛人端*/
    func checkoutMyHeightLight(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.heightLight, key: "MyHeightLight") { (data) in
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
        
    }
    /**查询所有状态信息 牛人端*/
    
    func checkoutMyStatus(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDJobfinderAPI.query.status, key: "MyStatusInfo") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    /**查询所有公司信息*/
    
    func checkoutCompanySize(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.companySize, key: "CompanySizeInfo") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    func checkoutCompanySizeJobfinder(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.companySizeJobfinder, key: "AllCompanySizeInfoJobfinder") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    
    /**查询所有牛人端常见问题*/
    func checkoutJobfinderQuest(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.commentJobFinderQuest, key: "CommentJobfinderQuestInfo") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    /**查询所有 Boss 端常见问题*/
    
    func checkoutBossQuest(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.commentBossQuest, key: "CommentBossQuestInfo") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    //
    func creatCache(api:String,key:String?,closure:(data:Array<NSDictionary>)->Void) {
        
        //        manger.userCacheManger?.diskCache.removeAllObjects()
        if key != nil && (manger.userCacheManger?.containsObjectForKey(key!))! == true {
            
            LDUserInfo.sharedInstance.readFromCache(key!, success: { (key, value) in
                closure(data: value as! Array<NSDictionary>)
                }, failure: nil)
            
        } else {
            
            LDNetWorkManager.sharedInstance.GET(api, header: nil,
                                                success: { (value) in
                                                    let data = value as! Array<NSDictionary>
                                                    if key != nil {
                                                        LDUserInfo.sharedInstance.saveToCache(key!, value: data, success: nil)
                                                    }
                                                    closure(data: data)
                                                    
            }) { (data, error) in
                debugPrint(data)
            }
        }
    }
    // 举 报 ID 和 对 应 的 名 称
    func checkoutReportJobfinderList(closure:(data:Array<NSDictionary>)->Void) {
        
        creatCache(LDCommentAPI.reportJobfinder, key: "checkoutReportJobfinderList") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    //
    func checkoutBossHeightLight(closure:(data:Array<NSDictionary>)->Void){
        
        creatCache(LDCommentAPI.queryEmploryHeightLightTags, key: "checkoutBossHeightLightList") { (data) in
            
            dispatch_async(dispatch_get_main_queue(), {
                closure(data: data)
            })
        }
    }
    
    /**查询 省市区 */
    func checkoutThridPlace(closure:(data:NSDictionary)->Void) {
        
        let api = LDCommentAPI.thridPlace
        let key  = api
        
        if manger.userCacheManger?.containsObjectForKey(key) == true {
            
            LDUserInfo.sharedInstance.readFromCache(key, success: { (key, value) in
                
                closure(data: value as! NSDictionary)
            }, failure: nil)
        } else {
            
            LDNetWorkManager.sharedInstance.GETJSON(api, success: { (value) in
                
                LDUserInfo.sharedInstance.saveToCache(key, value: value, success: nil)
                closure(data: value )
            }) { (data, error) in
                
            }
        }
    }
}
